/***
 *  Copyright (c) 2012 yanghui
 *  Author: yanghui1986527@gmail.com
 *  http://code.google.com/p/gravatar4android/
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.snowdream.gravatar4android.exception;

import java.util.ArrayList;

import android.text.TextUtils;


public class GravatarException extends Exception {

    private static final long serialVersionUID = -8302233862909496099L;

    private static ArrayList<String> errorMessageList = getErrorMessageList();

    /**
     * error code 
     * The email address is not valid.
     */
    public static final int EMAIL_INVAILD = 0;
    
    /**
     * error code 
     * Failed to get the profile
     */
    public static final int GET_GRAVATAR_PROFILE_FAILED = 1;

    private int errorCode = -1;
    private String errorMsg = "Unknown Error";

    /**
     * inner error message list
     * 
     * @return inner error message list
     */
    private static ArrayList<String> getErrorMessageList(){
        ArrayList<String> errorMsgList = new ArrayList<String>();
        errorMsgList.add(EMAIL_INVAILD, "The email address is not valid.");
        errorMsgList.add(GET_GRAVATAR_PROFILE_FAILED, "Failed to get the gravatar profile.");

        return errorMsgList;
    }

    /**
     * construct a GravatarException with the errorCode
     * 
     * @param errorCode the error code
     */
    public GravatarException(int errorCode) {
        super();
        this.errorCode = errorCode;
        if(errorCode < errorMessageList.size()){
            this.errorMsg = errorMessageList.get(errorCode);
        }

    }

    /**
     * construct a GravatarException with the errorCode and the detail message.
     * 
     * @param errorCode the error code
     * @param detailMessage the detail message
     */
    public GravatarException(int errorCode,String detailMessage) {
        super(detailMessage);
        this.errorCode = errorCode;

        if(!TextUtils.isEmpty(detailMessage)){
            this.errorMsg = detailMessage;
        }else{
            if(errorCode < errorMessageList.size()){
                this.errorMsg = errorMessageList.get(errorCode);
            }
        }
    }

    /**
     * get the error code
     * 
     * @return the error code
     */
    public int getErrorCode(){
        return this.errorCode;
    }

    /**
     * get the error message
     * 
     * @return the error message
     */
    public String getErrorMessage(){
        return this.errorMsg;
    }
}